<!--电子围栏页 GeofenceView.vue-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input @keydown="load_geofence" v-model="search_geofence_form.name" placeholder="请输入" style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="search_geofence_form.status" @change="load_geofence">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset_search">重置</el-button>
        <el-button type="primary" @click="load_geofence">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
     <!-- stripe:斑马纹/条形纹   -->
    <el-table stripe :data="geofence_arr">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch @change="change_geofence_status(scope.row.id,scope.row.status)" inactive-value="0" active-value="1" v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="load_vehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="delete_geofence(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="show_unbind_vehicle_list">添加绑定车辆</el-button>
    <el-table :data="bind_vehicle_arr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="remove_from_geofence(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog :before-close="handle_close" title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="unbind_vehicle_arr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bind_new_vehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import {ElMessage} from "element-plus";
import axios from "axios";
import router from "@/router"

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

/*搜索表单*/
const search_geofence_form = ref({name:'',status:''})
/*围栏数组*/
const geofence_arr = ref([])
/*已绑定车辆数组*/
const bind_vehicle_arr = ref([])
/*可绑定车辆数组*/
const unbind_vehicle_arr = ref([])
/*加载围栏*/
const load_geofence = ()=>{
  let data = qs.stringify(search_geofence_form.value)
  axios.get(BASE_URL+'/v1/geofence/select?'+data)
      .then((response)=>{
        if(response.data.code===2000){
          geofence_arr.value=response.data.data
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}
/*重置搜索*/
const reset_search = ()=>{
  search_geofence_form.value={name:'',status:''}
  load_geofence()

}

onMounted(()=>{
  load_geofence()
})
/*全局变量保存当前围栏的id*/
var global_geofence_id;
/*加载已绑定车辆*/
const load_vehicle =(geofenceId)=>{
  global_geofence_id=geofenceId
  bindVehicleDialogVisible.value=true
  axios.get(BASE_URL+'/v1/vehicle/select?geofenceId='+geofenceId)
      .then((response)=>{
        if(response.data.code===2000){
          bind_vehicle_arr.value=response.data.data
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}

/*移除当前围栏已绑定状态*/
const remove_from_geofence =(id)=>{
  //Restful风格
  axios.post(BASE_URL+'/v1/vehicle/unbind/'+id)
      .then((response)=>{
        if(response.data.code===2000){
          ElMessage.success('成功移除')
          load_vehicle(global_geofence_id)
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}
/*显示所有未绑定围栏车辆列表*/
const show_unbind_vehicle_list = ()=>{
  AddBindVehicleDialogVisible.value=true
  axios.get(BASE_URL+'/v1/vehicle/select?geofenceBindStatus=0')
      .then((response)=>{
        if(response.data.code===2000){
          unbind_vehicle_arr.value=response.data.data
        }else{
          ElMessage.error(response.data.msg)
        }
      })

}

const bind_new_vehicle = (id)=>{
  axios.post(BASE_URL+'/v1/vehicle/bind/'+id+'/'+global_geofence_id)
      .then((response)=>{
        if(response.data.code===2000){
          ElMessage.success('绑定成功')
          /*每绑定一辆车当前可绑定车辆列表刷新*/
          show_unbind_vehicle_list()
          /*每绑定一辆车，重新加载围栏上已经绑定的车辆，调用次数太多，服务器开销太大*/
          //load_vehicle(global_geofence_id)

        }else{
          ElMessage.error(response.data.msg)
        }
      })
}
const handle_close = ()=>{
  AddBindVehicleDialogVisible.value=false
  load_vehicle(global_geofence_id)
}
const change_geofence_status=(id,status)=>{
  let data = qs.stringify({id:id,status:status})
  axios.post(BASE_URL+'/v1/geofence/save/',data)
      .then((response)=>{
        if(response.data.code===2000){
          ElMessage.success('修改成功')
        }else{
          ElMessage.error(response.data.msg)
        }
      })
}
const delete_geofence = (id)=>{

  if(confirm('将解绑该围栏上所有的车辆，确认删除吗？')){

    axios.post(BASE_URL+'/v1/geofence/deleteGeofence/'+id)
        .then((response)=>{
          if(response.data.code===2000){
            ElMessage.success('删除成功')
            load_geofence()
          }else{
            ElMessage.error('请先移除该围栏上所有已绑定的车辆')
          }
        })
  }

}

</script>

<style scoped>
</style>